package org.cs.web;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.cs.mode.UserDetail;

public class UserDetailQuery extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public UserDetailQuery() {
        super();
    }

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Connection conn = null;
		PreparedStatement  pstmt=null;
		List<UserDetail> list= new ArrayList<UserDetail>();
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/cs?useUnicode=true&characterEncoding=utf-8","root","123123");
			String sql="SELECT * FROM user_detail";
			pstmt=conn.prepareStatement(sql);
			ResultSet rs=pstmt.executeQuery();
			while(rs.next()){
				UserDetail us = new UserDetail();
				us.setId(rs.getInt("id"));
				us.setNickName(rs.getString("nick_name"));
				us.setEmail(rs.getString("email"));
				us.setPasswd(rs.getString("passwd"));
				us.setIntro(rs.getString("intro"));
				us.setSex(rs.getInt("sex"));
				list.add(us);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				pstmt.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		request.setAttribute("list", list);
		request.getRequestDispatcher("cs_login/user_detail_list.jsp").forward(request, response);
	}

}
